home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: normanb@halcyon.com (Norm Bryar)
- Newsgroups: comp.lang.c++
- Subject: Re: Pointer to Functions and Calls thereof??
- Date: Thu, 18 Apr 1996 15:58:15 GMT
- Organization: Northwest Nexus Inc.
- Message-ID: <4l5op9$bls@news.halcyon.com>
- References: <Dq017E.DL6@latcs1.lat.oz.au>
- NNTP-Posting-Host: blv-pm3-ip12.halcyon.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- woelkerl@lion.cs.latrobe.edu.au (Eric Woelkerling) wrote:
-
- > I'm having some trouble getting a pointer to a function to work...
-
- > So far - I've only got as far as..
-
- > void func1(void){various bits}
- > void func2(void){various bits}
-
- > void main()
- > { void *a[1];
-
- > a[0]=func1;
- > a[1]=func2;
-
- > (*a[0])();
- > (*a[1])();
- > }
-
- > ...
- > Please email me directly.. Thanks!
- > Eric Woelkerling..
- > woelkerl@lion.lat.oz.au
-
-
- Eric;
- C++ likes type-safety. First thing I'd do is typedef the function
- pointers you will be putting into an array, an array of that type, of
- course. Then you can call the functions with (a[n])(args).
-
- Lastly, your array was defined as one element, but you put in two.
-
-
- typedef void (*fptr)(void);
-
- void func1(void)
- { cout << "func1\n"; }
- void func2(void)
- { cout << "func2\n"; }
-
- void main()
- {
- fptr a[2];
-
- a[0]=func1;
- a[1]=func2;
-
- (a[0])( ); // calls func1
- (a[1])( ); // calls func2
- }
-
- Good luck.
- --Norm
-
-